import { Component, OnInit, ViewChild } from '@angular/core';
import { DeviceService } from '../device-list/device.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { DeviceInspectionTemplateService } from './device-inspection-template.service';
import { DeviceInspectionTemplateEditComponent } from './device-inspection-template-edit/device-inspection-template-edit.component';
import { DeviceInspectionTemplateItemEditComponent } from './device-inspection-template-item-edit/device-inspection-template-item-edit.component';

@Component({
  selector: 'app-device-inspection-template',
  templateUrl: './device-inspection-template.component.html',
  styles: []
})
export class DeviceInspectionTemplateComponent implements OnInit {

  loading = false;

  // 列表数据
  dataList = [];

  @ViewChild('deviceInspectionTemplateEditComponent', {static: false})
  public deviceInspectionTemplateEditComponent: DeviceInspectionTemplateEditComponent;

  @ViewChild('deviceInspectionTemplateItemEditComponent', {static: false})
  public deviceInspectionTemplateItemEditComponent: DeviceInspectionTemplateItemEditComponent;

  // 分页参数
  pageData = {page: 0, size: 10};
  // 查询条件
  searchBeans = [{
    attributeName: 'code',
    searchType: 'LIKE',
    attributeValue: ''
  }, {
    attributeName: 'enable',
    searchType: 'EQ',
    attributeValue: ''
  }];

  constructor(private deviceGroupSrv: DeviceService,
              private deviceInspectionTemplateSrv: DeviceInspectionTemplateService,
              private msgSrv: NzMessageService,) {
  }

  ngOnInit(): void {
    this.loadListData();
  }

  delete(id?) {
    this.loading = true;
    this.deviceGroupSrv.delete(id).subscribe((res) => {
      this.loading = false;
      if (res.code === 1) {
        this.msgSrv.success('删除完成');
      } else {
        this.msgSrv.error(res.message);
      }
    });
    console.log(id);
  }

  onSmt() {
    this.loadListData();
  }

  cancel() {
    this.msgSrv.success('已取消');
  }

  /**
   * 编辑检验模板
   * @param record
   */
  edit(record ?: any): void {
    this.deviceInspectionTemplateEditComponent.edit(record);
  }

  open(){
    this.deviceInspectionTemplateEditComponent.open();
  }

  /**
   * 维护检验项
   * @param record
   */
  openEditItem(record ?: any): void {

    console.log(record);
    this.deviceInspectionTemplateItemEditComponent.edit(record);

  }

  /**
   * 清除查询参数
   */
  clearParam() {

    this.searchBeans.forEach((item) => {
      item.attributeValue = '';
    });
  }

  /**
   * 加载列表数据
   */
  loadListData() {

    this.loading = true;

    const searchData = {
      page: this.pageData.page,
      size: this.pageData.size,
      searchFilter: {
        searchBeans: this.searchBeans
      }
    };

    this.deviceInspectionTemplateSrv.findPage(searchData).subscribe((res) => {

      this.loading = false;

      if (res.code === 1) {
        this.dataList = res.data.content;
      } else {
        this.msgSrv.error(res.message);
      }

    });
  }

  _onReuseInit() {
    this.loadListData();
  }
}
